home *** CD-ROM | disk | FTP | other *** search
/ Cracking 2 / Cracking II..iso / Tools / TRW2000 for Win9x v1.22 / PLUGSDK / STACK / STACK.CPP < prev   
Encoding:
C/C++ Source or Header  |  2000-03-07  |  4.2 KB  |  187 lines

  1. // Stack Plugs for TRW2000
  2. // Copyright (C) , 1999 ,
  3. //
  4. // Author:
  5. //           Zhunanhao  , reached at nhzhu@163.net
  6. //
  7. // History :
  8. //    2000.2.24  Zhunanhao write origin code    
  9. //
  10. // Note:
  11. //           This is only a DEMO! Please modify it to improve!
  12. //
  13.  
  14. #include <wdm.h>
  15. #include "..\INCLUDE\PLUGS.H" 
  16.  
  17. // prototypes
  18.  
  19. EXC NTSTATUS
  20. DriverEntry(IN PDRIVER_OBJECT DriverObject, IN PUNICODE_STRING RegistryPath);
  21.  
  22. VOID
  23. PLUGS_Unload(IN PDRIVER_OBJECT DriverObject);
  24.  
  25.  
  26. NTSTATUS
  27. DriverEntry(IN PDRIVER_OBJECT DriverObject, IN PUNICODE_STRING RegistryPath)
  28. {
  29.     NTSTATUS    ntStatus = STATUS_SUCCESS;
  30.  
  31.     DriverObject->DriverUnload = PLUGS_Unload;
  32.  
  33.     return ntStatus;
  34. }
  35.  
  36. VOID
  37. PLUGS_Unload(IN PDRIVER_OBJECT DriverObject)
  38. {
  39. }
  40.  
  41. /****************** WDM Rountine End ****************/
  42.  
  43. // prototype
  44.  
  45. BOOL cmd_STACK( int argc,char** argv ) ;
  46.  
  47. // end
  48.  
  49. PLUGS_API* api = 0 ;
  50. // Call TRW2000 API must like this:
  51. //      api->Add_Command ( ) ;
  52.  
  53. EXC EXPORT BOOL Plugs_Init ( PLUGS_API* plugsapi)
  54. {
  55.     api=plugsapi;    
  56.  
  57.     api->Add_Command (    "STACK", 0,
  58.                         "Display call stack", 0,
  59.                         cmd_STACK );
  60.  
  61.     api->dprintf ( "STACK Plugs Ver 0.01 Initialized..." ) ;
  62.  
  63.     return TRUE ;
  64. }
  65.  
  66. EXC EXPORT BOOL Plugs_Exit ( )
  67. {
  68.     return TRUE ;
  69. }
  70.  
  71. /***************** Command ***************/
  72.  
  73. //#define DEBUG
  74. // Command : STACK
  75.  
  76. BOOL cmd_STACK( int argc,char**argv )
  77. {
  78.     DWORD    EBP;
  79.     DWORD    r;
  80.     PSTR    s;
  81.  
  82.     char    buf[20];
  83.  
  84.     ADDRE    addr,addr2;
  85.  
  86.     if( argc!=0 )
  87.         return FALSE ;
  88.  
  89. #ifdef    DEBUG    
  90.     api->Begin_Nest_VMM_Exec();
  91. #endif
  92.     api->CurCSEIP(&addr2);
  93.     if( api->ifLinear(&addr2)==FALSE ) {
  94.         api->dprintf ( "Sorry, now STACK only support Win32&VxD linear address." ) ;
  95.         return TRUE;
  96.     }
  97.         
  98.     addr._off=api->pUser->CRS.Client_EBP;
  99.     addr._seg=api->pUser->CRS.Client_SS;
  100.     addr._mod=0;
  101.     
  102.     EBP=addr._off;    
  103.     api->dprintf("Address\t\tSymbol/Owner");
  104.     
  105.     while(1) {
  106.  
  107.         addr._off=EBP+4;
  108.         if( api->PeekD(&addr,&r)==FALSE )
  109.             break;
  110.         
  111.         addr._off=EBP;
  112.         if( api->PeekD(&addr,&EBP)==FALSE )
  113.             break;
  114.             
  115.         if( EBP<=addr._off )
  116.             break;
  117.         
  118.         addr2._off=r;    
  119.         s=api->GetSymbol(&addr2,0x400);
  120.         if( s==0 ) 
  121.             s=api->GetSymbolArea(&addr2);
  122.         if( s==0 ) {
  123.             api->sprintf(buf,"%08X",r);
  124.             s=buf;
  125.         }        
  126.         api->dprintf("%08X    %s",r,s);
  127.     }    
  128.     
  129. #ifdef    DEBUG    
  130.     api->End_Nest_VMM_Exec();
  131. #endif    
  132.  
  133.     return TRUE ;
  134. }
  135.  
  136. /*
  137. This document is from DDK 2000 document.
  138.  
  139. Anatomy of a Stack Trace
  140. The following is a register dump from the kernel debugger. It contains information
  141. about what is stored in the stack:
  142.  
  143. kd> r  [This command dumps the information from a register.]
  144. eax=c0000018 ebx=80621828 ecx=00000000 edx=807da761 esi=e12e0e28 edi=e12df868
  145. eip=f28343b3 esp=f2c132cc ebp=f2c132f0 iopl=0         nv up ei pl zr na po nc
  146. cs=0008  ss=0010  ds=0023  es=0023  fs=0030  gs=0000  efl=00000246
  147. VIDEOPRT!pVideoPortReportResourceList+0x263:
  148. f28343b3 8db340010000     lea     esi,[ebx+0x140]   ds:0023:80621968=00000000
  149.  
  150. The registers are used as follows: 
  151.  
  152. esp = stack pointer 
  153. ebp = base pointer 
  154. eip = instruction pointer 
  155. eax, ebx, ecx, edx = general purpose registers for storing intermediate results 
  156. edi, esi = often used as general registers 
  157. A stack trace could be diagramed in the following manner: 
  158.  
  159. Arguments 
  160. Return Address 
  161. Caller's EBP 
  162. Local Variables 
  163. The stack trace displayed on a kernel debugger is written in assembly code. A simple 
  164. set of instructions in assembly code could look like this:
  165.  
  166. push ebp            ;Places the caller's base pointer (ebp) on to the stack
  167. mov ebp,esp            ;Sets the base pointer (ebp) equal to the stack pointer (esp)
  168. mov eax, [ebp+8]    ;Grab the value of the first argument off the stack and store
  169.                     ;it in eax
  170. add eax, [ebp+c]    ;Add the second argument's value to the value in eax
  171. pop ebp                ;Restore the caller's base pointer
  172. ret 8                ;Return to the calling function and remove 8 bytes from the stack
  173.  
  174. The compiler can optimize this code, simplifying it so that it is shorter and more 
  175. direct. An optimized routine might look like this: (Note how the code omits references 
  176. to the base pointer.)
  177.  
  178. mov eax, [esp+4]
  179. add eax, [esp+8]
  180. ret 8
  181. Finally here's how it would look in C:
  182.  
  183. ULONG Add(ULONG a, ULONG b)
  184. {
  185.     return a + b;
  186. }
  187. */